package ru.sushka.lunch.dao.psql;

import java.sql.ResultSet;
import java.sql.SQLException;

import ru.sushka.lunch.enitiy.Customer;

public class PsqlCustomerDao extends PsqlDao<Customer>{

    private final String tableName = "customers";
    
    @Override
    protected String getTableName() {
        return tableName;
    }

    @Override
    protected String getSelectSqlQuery(int id) {
        return "select * from " + tableName + " where id = " + id;
    }

    @Override
    protected String getDeleteSqlQuery(int id) {
        return "delete from " + tableName + " where id = " + id;
    }

    @Override
    protected String getInsertSqlQuery(Customer persistentce) {
        return "insert into " + tableName + "(firstname, lastname) values('" + 
                                persistentce.getFirstname() + "','" + persistentce.getLastname() +"')";
    }

    @Override
    protected String getUpdateSqlQuery(Customer persistentce) {
        return "update customers set id=" + persistentce.getId() + "," +
                                        "firstname = '"+persistentce.getFirstname()+"',"+
                                        "lastname ='"+persistentce.getLastname()+"' "+
                                        "where id=" + persistentce.getId();
    }

    @Override
    protected Customer parseResultSet(ResultSet rs) throws SQLException{
        int id = rs.getInt(1);
        String firstname = rs.getString(2);
        String lastname = rs.getString(3);
        Customer customer = new Customer(firstname, lastname);
        customer.setId(id);
        return customer;
    }

}
